﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;

namespace GCard
{
    public partial class GCard : Form
    {
        public GCard()
        {
            InitializeComponent();
            FormInitial();

            FormTransfer(0);
            change += new myHandle(homePage.ServoStatus);
            change += new myHandle(homePage.PrfEncPos);
            change();
        }
        GtsCard gCard = new GtsCard();
        private void GCard_Load(object sender, EventArgs e)
        {
            //卡与拓展模块都需要进行打开操作及加载各自的配置文件
            short sRtn;
            bool initB = gCard.OpenCardInit(0, "GTS800.cfg");
            if (initB)
            {
                MessageBox.Show("板卡初始化失败");
                return;
            }
            sRtn = gts.mc.GT_OpenExtMdl(0, "gts.dll");  //拓展模块与gts卡函数都在gts.dll其中
            sRtn = gts.mc.GT_ResetExtMdl(0);            //复位拓展模块
            sRtn = gts.mc.GT_LoadExtConfig(0, "ExtMdl.cfg");//加载拓展模块配置文件
            sRtn = gts.mc.GT_ClrSts(0,1,8);
        }

        //窗口显示管理
        IOStatus fileForm = new IOStatus();
        ChartForm chartForm = new ChartForm();
        HomePage homePage = new HomePage();
        XMLForm xmlForm = new XMLForm();
        PortForm portForm = new PortForm();
        TcpForm tcpForm = new TcpForm();

        public void FormInitial()
        {
            homePage.TopLevel = false;
            homePage.Dock = DockStyle.Fill;
            panel_Display.Controls.Add(homePage);

            fileForm.TopLevel = false;
            fileForm.Dock = DockStyle.Fill;
            panel_Display.Controls.Add(fileForm); 
   
            chartForm.TopLevel = false;
            chartForm.Dock = DockStyle.Fill; 
            panel_Display.Controls.Add(chartForm);

            xmlForm.TopLevel = false;
            xmlForm.Dock = DockStyle.Fill;
            panel_Display.Controls.Add(xmlForm);

            portForm.TopLevel = false;
            portForm.Dock = DockStyle.Fill;
            panel_Display.Controls.Add(portForm);

            tcpForm.TopLevel = false;
            tcpForm.Dock = DockStyle.Fill;
            panel_Display.Controls.Add(tcpForm);
             
        }
        //0:homePage    1:fileForm  2:chartForm  3:xmlForm  4:portForm
        public void FormTransfer(short index)
        {
            switch (index)
            {
                case 0:
                    homePage.Visible = true;
                    fileForm.Visible = false;
                    chartForm.Visible = false;
                    xmlForm.Visible = false;
                    portForm.Visible = false;
                    tcpForm.Visible = false;
                    break;
                case 1:
                    homePage.Visible = false;
                    fileForm.Visible = true;
                    chartForm.Visible = false;
                    xmlForm.Visible = false;
                    portForm.Visible = false;
                    tcpForm.Visible = false;
                    break;
                case 2:
                    homePage.Visible = false;
                    fileForm.Visible = false;
                    chartForm.Visible = true;
                    xmlForm.Visible = false;
                    portForm.Visible = false;
                    tcpForm.Visible = false;
                    break;
                case 3:
                    homePage.Visible = false;
                    fileForm.Visible = false;
                    chartForm.Visible = false;
                    xmlForm.Visible = true;
                    portForm.Visible = false;
                    tcpForm.Visible = false;
                    break;
                case 4:
                    homePage.Visible = false;
                    fileForm.Visible = false;
                    chartForm.Visible = false;
                    xmlForm.Visible = false;
                    portForm.Visible = true;
                    tcpForm.Visible = false;
                    break;
                case 5:
                    homePage.Visible = false;
                    fileForm.Visible = false;
                    chartForm.Visible = false;
                    xmlForm.Visible = false;
                    portForm.Visible = false;
                    tcpForm.Visible = true;
                    break;
                default: break;
            }
        }
        public delegate void myHandle();//切换后伺服状态与位置循环扫描
        public event myHandle change;


        public delegate void myHandle1();//切换后IO扫描
        public event myHandle1 change1;
        public event myHandle1 ExtMdlScan;//
        //主要界面切换
        private void btn_Login_Click(object sender, EventArgs e)
        {
            FormTransfer(0);
            change += new myHandle(homePage.ServoStatus);
            change += new myHandle(homePage.PrfEncPos);
            change();
        }

        private void btn_File_Click(object sender, EventArgs e)
        {
            FormTransfer(1);
            change1 += new myHandle1(fileForm.IOScan);
            change1();
            ExtMdlScan += new myHandle1(fileForm.ExtMdlScan);
            ExtMdlScan();
        }

        private void btn_chart_Click(object sender, EventArgs e)
        {
            FormTransfer(2);
        }

        private void btn_xmlForm_Click(object sender, EventArgs e)
        {
            FormTransfer(3);
        }

        private void btn_portForm_Click(object sender, EventArgs e)
        {
            FormTransfer(4);
        }

        private void btn_tcpForm_Click(object sender, EventArgs e)
        {
            FormTransfer(5);
        }

        private void MainForm_Closing(object sender, FormClosingEventArgs e)
        {
            short sRtn;
            sRtn = gts.mc.GT_Close(0);//关闭软件前先关闭卡和拓展模块
            sRtn = gts.mc.GT_CloseExtMdl(0);
        }

        LoginForm loginForm;
        private void btn_loginIn_Click(object sender, EventArgs e)
        {
            loginForm = new LoginForm();  
            loginForm.Show();
            loginForm.Visible = true;
        }
    }
}
